package com.ruoyi.tongyan.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.tongyan.domain.TClosure;
import com.ruoyi.tongyan.domain.TDeclaration;
import com.ruoyi.tongyan.domain.TProject;
import com.ruoyi.tongyan.mapper.*;
import com.ruoyi.tongyan.service.TReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TReviewServiceImpl implements TReviewService {

    @Autowired
    private TReviewMapper tReviewMapper;
    @Autowired
    private TDeclarationMapper tDeclarationMapper;
    @Autowired
    private TProjectMapper tProjectMapper;
    @Autowired
    private TDeclarationProjectMapper tDeclarationProjectMapper;
    @Autowired
    private TClosureMapper tClosureMapper;

    /**
     * 申报和专家评审关联
     * @param declarationIds
     * @param userId
     * @return
     */
    @Override
    public int assignDeclaration(Long[] declarationIds, Long userId) {
        return tReviewMapper.assignDeclaration(declarationIds,userId);
    }

    /**
     * 专家通过申报
     * @param declarationId
     * @return
     */
    @Override
    @Transactional
    public int passDeclaration(Long declarationId) {
        // 修改申报状态为0已通过
        TDeclaration declaration = tDeclarationMapper.selectTDeclarationByDeclarationId(declarationId);
        declaration.setStatu("0");
        tDeclarationMapper.updateTDeclaration(declaration);

        // 审核通过，可以立项，生成相应项目project_Id
        // 添加项目的id，名称，负责人和立项金额，项目状态
        TProject tProject = new TProject();
        tProject.setProjectName(declaration.getProjectName());
        tProject.setFundingAmount(declaration.getBudget());
        tProject.setPrincipalInvestigator(declaration.getApplicant());
        tProject.setStatu("0");
        tProjectMapper.insertTProject(tProject);

        // 申报记录自动关联项目
        tDeclarationProjectMapper.insert(declaration.getDeclarationId(), tProject.getProjectId());

        // 返回成功状态码
        return 1; // 1 表示成功
    }

    /**
     * 专家未通过申报
     * @param declarationId
     * @return
     */
    @Override
    public int rejectDeclaration(Long declarationId) {
        TDeclaration tDeclaration = tDeclarationMapper.selectTDeclarationByDeclarationId(declarationId);
        //将申报状态设置为2未通过
        tDeclaration.setStatu("2");
        return tDeclarationMapper.updateTDeclaration(tDeclaration);
    }



    /**
     * 结题和专家评审关联
     * @param closureIds
     * @param userId
     * @return
     */
    @Override
    public int assignClosure(Long[] closureIds, Long userId) {
        return tReviewMapper.assignClosure(closureIds,userId);
    }

    /**
     * 专家通过结题
     * @param closureId
     * @return
     */
    @Override
    public int passClosure(Long closureId) {
        TClosure tClosure = tClosureMapper.selectTClosureByClosureId(closureId);
        //设置审核状态为0通过
        tClosure.setReviewStatu("0");
        return tClosureMapper.updateTClosure(tClosure);
    }

    /**
     * 专家未通过结题
     * @param closureId
     * @return
     */
    @Override
    public int rejectClosure(Long closureId) {
        TClosure tClosure = tClosureMapper.selectTClosureByClosureId(closureId);
        //设置审核状态为2未通过
        tClosure.setReviewStatu("2");
        return tClosureMapper.updateTClosure(tClosure);
    }
}
